package com.transmit.conf;


import com.alibaba.fastjson.JSONObject;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.messages.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;

@Component
public class FastMinio {

    private static final Logger logger = LoggerFactory.getLogger(FastMinio.class);

//    @Value("${oss.url}")
    String ossurl;
//    @Value("${oss.accessKey}")
    String ossak;
//    @Value("${oss.secretKey}")
    String osssk;

    public InputStream get(String bucket, String name) throws Exception {

        MinioClient minioClient = new MinioClient(ossurl, ossak, osssk);
        // 使用putObject上传一个文件到存储桶中。
        InputStream stream = minioClient.getObject(bucket, name);
        if (stream != null) {
            return stream;
        } else {
            return null;
        }
    }

    public ObjectStat stat(String bucket, String name) {
        try {
            MinioClient minioClient = new MinioClient(ossurl, ossak, osssk);
            // 使用putObject上传一个文件到存储桶中。
            ObjectStat stat = minioClient.statObject(bucket, name);
            if (stat != null) {
                return stat;
            } else {
                return null;
            }

        } catch (Exception e) {
//            e.printStackTrace();
            logger.error("查找图片出错：" + e.getMessage());
        }
        return null;
    }

    public List<String> listfile(String bucket) {
        try {
            MinioClient minioClient = new MinioClient(ossurl, ossak, osssk);
            // 检查'my-bucketname'是否存在。
            boolean found = minioClient.bucketExists(bucket);
            List<String> list = new LinkedList<>();
            if (found) {
                // 列出'my-bucketname'里的对象
                Iterable<Result<Item>> myObjects = minioClient.listObjects(bucket);
                for (Result<Item> result : myObjects) {
                    Item item = result.get();
                    list.add(item.objectName());
                }
            } else {

            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String put(String bucket, String name, InputStream stream, String contenttype) {
        try {
            MinioClient minioClient = new MinioClient(ossurl, ossak, osssk);
            // 使用putObject上传一个文件到存储桶中。
            if (!minioClient.bucketExists(bucket)) {
                minioClient.makeBucket(bucket);
            }
            minioClient.putObject(bucket, name, stream, contenttype);
            stream.close();
            if (stream != null) {
                stream = null;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public String delete(String bucket, String name) {
        try {
            MinioClient minioClient = new MinioClient(ossurl, ossak, osssk);
            // 使用putObject上传一个文件到存储桶中。
            minioClient.removeObject(bucket, name);
            return null;

        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public String putJsonArray(String db, String table, List<JSONObject> list) {
        try {
            InputStream in_withcode = new ByteArrayInputStream(list.toString().getBytes(StandardCharsets.UTF_8));
            put(db, table, in_withcode, "application/json");
        } catch (Exception e) {
            return e.getMessage();
        }
        return null;
    }

    public String putfile(String db, String table, String file) {
        try {
            MinioClient minioClient = new MinioClient(ossurl, ossak, osssk);
            // 使用putObject上传一个文件到存储桶中。
            if (!minioClient.bucketExists(db)) {
                minioClient.makeBucket(db);
            }
            minioClient.putObject(db, table, file);

        } catch (Exception e) {
            return e.getMessage();
        }
        return null;
    }

    public void deleteBatch(String bucket, List<String> fileNames){
        if(fileNames != null && fileNames.size() > 0){
            for (String fileName : fileNames){
                if(!StringUtils.isEmpty(fileName)) {
                    delete(bucket, fileName);
                }
            }
        }
    }
}